package org.springcloud.plugin.framework.strategy.context;

import org.apache.commons.lang3.StringUtils;
import org.springcloud.plugin.common.constant.DiscoveryConstant;
import org.springcloud.plugin.framework.context.PluginContextHolder;
import org.springcloud.plugin.framework.strategy.wrapper.StrategyWrapper;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * author: yanggld
 * date: 2019/12/21 19:56
 * version: 1.0
 * description: AbstractStrategyContextHolder
 */
public abstract class AbstractStrategyContextHolder implements PluginContextHolder,StrategyContextHolder {

    @Autowired
    protected StrategyWrapper strategyWrapper;

    @Override
    public String getContext(String name) {
        return getHeader(name);
    }

    @Override
    public String getContextRouteVersion() {
        String versionValue = getContext(DiscoveryConstant.N_D_VERSION);
        if (StringUtils.isEmpty(versionValue)) {
            versionValue = getRouteVersion();
        }
        return versionValue;
    }

    @Override
    public String getContextRouteRegion() {
        String regionValue = getContext(DiscoveryConstant.N_D_REGION);
        if (StringUtils.isEmpty(regionValue)) {
            regionValue = getRouteRegion();
        }
        return regionValue;
    }


    @Override
    public String getContextRouteVersionWeight() {
        String versionWeightValue = getContext(DiscoveryConstant.N_D_VERSION_WEIGHT);
        if (StringUtils.isEmpty(versionWeightValue)) {
            versionWeightValue = getRouteVersionWeight();
        }
        return versionWeightValue;
    }

    @Override
    public String getContextRouteRegionWeight() {
        String regionWeightValue = getContext(DiscoveryConstant.N_D_REGION_WEIGHT);
        if (StringUtils.isEmpty(regionWeightValue)) {
            regionWeightValue = getRouteRegionWeight();
        }
        return regionWeightValue;
    }

    @Override
    public String getRouteRegion() {
        return strategyWrapper.getRouteRegion();
    }

    @Override
    public String getRouteVersion() {
        return strategyWrapper.getRouteVersion();
    }

    @Override
    public String getRouteVersionWeight() {
        return strategyWrapper.getRouteVersionWeight();
    }

    @Override
    public String getRouteRegionWeight() {
        return strategyWrapper.getRouteRegionWeight();
    }
}
